#include "vmidi_rtmidi.h"

#include <unistd.h>
#include <iostream>

using namespace VMidi;
using namespace std;



void test1 (VMidiInterface* inter) {
  try {
    int id;
    inter->createVirtualPort(VMIDI_PORT_INPUT, "VMIDI IN 1", id);
    inter->createVirtualPort(VMIDI_PORT_INPUT, "VMIDI IN 2", id);
    inter->createVirtualPort(VMIDI_PORT_INPUT, "VMIDI IN 3", id);
    inter->createVirtualPort(VMIDI_PORT_INPUT, "VMIDI IN 4", id);
    inter->createVirtualPort(VMIDI_PORT_INPUT, "VMIDI IN 5", id);

    inter->createVirtualPort(VMIDI_PORT_OUTPUT, "VMIDI OUT 1", id);
    inter->createVirtualPort(VMIDI_PORT_OUTPUT, "VMIDI OUT 2", id);
    inter->createVirtualPort(VMIDI_PORT_OUTPUT, "VMIDI OUT 3", id);
    inter->createVirtualPort(VMIDI_PORT_OUTPUT, "VMIDI OUT 4", id);
    inter->createVirtualPort(VMIDI_PORT_OUTPUT, "VMIDI OUT 5", id);

    (static_cast<VMidiImplCommon*>(inter))->dump();

    sleep(30);

    inter->destroyVirtualPort(VMIDI_PORT_INPUT, 3);
    inter->destroyVirtualPort(VMIDI_PORT_OUTPUT, 7);

    (static_cast<VMidiImplCommon*>(inter))->dump();

    inter->connectPorts (1,6);
    inter->connectPorts (1,8);
    inter->connectPorts (1,9);
    inter->connectPorts (1,10);

    inter->connectPorts (2,6);
    inter->connectPorts (2,9);

    inter->connectPorts (4,8);
    inter->connectPorts (4,10);

    inter->connectPorts (5,6);
    inter->connectPorts (5,8);
    inter->connectPorts (5,10);

    (static_cast<VMidiImplCommon*>(inter))->dump();

    inter->disconnectPorts(5,8);
    
    (static_cast<VMidiImplCommon*>(inter))->dump();

    inter->destroyVirtualPort(VMIDI_PORT_INPUT, 2);
    inter->destroyVirtualPort(VMIDI_PORT_OUTPUT, 10);

    (static_cast<VMidiImplCommon*>(inter))->dump();
  }
  catch (string& reason) {
    cerr << "Exception : " << reason << endl;
  }

}

void test2 (VMidiInterface* inter) {
  try {
    int id;
    inter->createVirtualPort(VMIDI_PORT_INPUT, "VMIDI IN 1", id);
    inter->createVirtualPort(VMIDI_PORT_OUTPUT, "VMIDI OUT 1", id);
    inter->createVirtualPort(VMIDI_PORT_OUTPUT, "VMIDI OUT 2", id);

    inter->connectPorts(1,2);
    inter->connectPorts(1,3);

    bool done = false;

    bool connected = true;

    while (!done) {
      cout << "Commands :" << endl;
      cout << "c : connect input to outputs" << endl;
      cout << "d : disconnect input from outputs" << endl;
      cout << "q : quit" << endl;

      char ch;
      cin.get(ch);
      cin.ignore();

      switch (ch) {
        case 'c' : 
          if (!connected) {
            inter->connectPorts(1,2);
            inter->connectPorts(1,3);
            cout << endl << "Ports connected." << endl;
            connected = true;
          }
          break;
        case 'd' :
          if (connected) {
            inter->disconnectPorts(1,2);
            inter->disconnectPorts(1,3);
            cout << endl << "Ports disconnected." << endl;
            connected = false;
          }
          break;
        case 'q' :
          done = true;
          break;
        default:
          break;
      }
    } // End menu event loop.
    cout << "Quitting." << endl;
  }
  catch (string& reason) {
    cerr << "Exception : " << reason << endl;
  }
}

int main () {
  VMidiInterface* inter = VMidiFactory::getInstance();
  inter->initialize();

  //test1(inter);
  test2(inter);

  inter->finalize();
  delete inter;
}

